package cn.iocoder.yudao.module.jikong.controller.admin.resource;

import cn.iocoder.yudao.module.jikong.controller.admin.project.vo.JkProjectPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.project.vo.JkProjectRespVO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.project.JkProjectDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.resource.JkResourceDO;
import cn.iocoder.yudao.module.jikong.service.resource.JkResourceService;

@Tag(name = "管理后台 - 样本来源")
@RestController
@RequestMapping("/jikong/jk-resource")
@Validated
public class JkResourceController {

    @Resource
    private JkResourceService jkResourceService;

    @PostMapping("/create")
    @Operation(summary = "创建样本来源")
    @PreAuthorize("@ss.hasPermission('jikong:jk-resource:create')")
    public CommonResult<Long> createJkResource(@Valid @RequestBody JkResourceSaveReqVO createReqVO) {
        return success(jkResourceService.createJkResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样本来源")
    @PreAuthorize("@ss.hasPermission('jikong:jk-resource:update')")
    public CommonResult<Boolean> updateJkResource(@Valid @RequestBody JkResourceSaveReqVO updateReqVO) {
        jkResourceService.updateJkResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样本来源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:jk-resource:delete')")
    public CommonResult<Boolean> deleteJkResource(@RequestParam("id") Long id) {
        jkResourceService.deleteJkResource(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样本来源")
                @PreAuthorize("@ss.hasPermission('jikong:jk-resource:delete')")
    public CommonResult<Boolean> deleteJkResourceList(@RequestParam("ids") List<Long> ids) {
        jkResourceService.deleteJkResourceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样本来源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:jk-resource:query')")
    public CommonResult<JkResourceRespVO> getJkResource(@RequestParam("id") Long id) {
        JkResourceDO jkResource = jkResourceService.getJkResource(id);
        return success(BeanUtils.toBean(jkResource, JkResourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样本来源分页")
    @PreAuthorize("@ss.hasPermission('jikong:jk-resource:query')")
    public CommonResult<PageResult<JkResourceRespVO>> getJkResourcePage(@Valid JkResourcePageReqVO pageReqVO) {
        PageResult<JkResourceDO> pageResult = jkResourceService.getJkResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样本来源 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:jk-resource:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkResourceExcel(@Valid JkResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkResourceDO> list = jkResourceService.getJkResourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样本来源.xls", "数据", JkResourceRespVO.class,
                        BeanUtils.toBean(list, JkResourceRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得样本来源下拉框")
    public CommonResult<PageResult<JkResourceRespVO>> getJkResourceList(@Valid JkResourcePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkResourceDO> pageResult = jkResourceService.getJkResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkResourceRespVO.class));
    }


}
